package br.sed.theenemy.presentation.action;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import br.sed.theenemy.business.exception.DBException;
import br.sed.theenemy.business.exception.InvalidUniqueBeanException;
import br.sed.theenemy.business.services.Services;
import br.sed.theenemy.business.utils.Utils;
import br.sed.theenemy.model.bean.UserRequest;
import br.sed.theenemy.presentation.EnemyConstants;
import br.sed.theenemy.presentation.EnemyErrorConstants;
import br.sed.theenemy.presentation.action.json.JSONBaseAction;
import br.sed.theenemy.presentation.action.json.exception.ExpectedException;
import br.sed.theenemy.presentation.form.RegisterRequestForm;


/**
 * 
 * @author Paulo Mendes
 * @email paulo.mendes87@gmail.com
 * 
 * Servlet to register an user request 
 */
public class RegisterUserRequestAction extends JSONBaseAction {
	
	private static final long serialVersionUID = 5765171471260489942L;
	
	private static DateFormat DATE_FORMAT = new SimpleDateFormat(EnemyConstants.SIMPLE_DATE_FORMAT);
	
	@Override
	protected Object executeJSON(final ActionMapping mapping,final HttpServletRequest request, final ActionForm form) throws ExpectedException{
        Services services = Services.getInstance();
		RegisterRequestForm regResqForm = (RegisterRequestForm) form;
        
		if (!form.validate(mapping, request).isEmpty()) {
		    throw new ExpectedException(EnemyErrorConstants.INCORRECT_PARAM);
		}
		
		List<String> errors =  new ArrayList<String>();
		errors = validate(regResqForm);
		String password = regResqForm.getPassword();
		String confPassword = regResqForm.getConfirmPassword();
		if(!password.equals(confPassword)){
			errors.add("Please type the same password twice");
		}
		if (errors.size()!=0){
			throw new ExpectedException(EnemyErrorConstants.INCORRECT_PARAM, errors);
		}
		String dateStr = regResqForm.getBornDate();
		Date date = null;
		try {
			date = DATE_FORMAT.parse(dateStr);
		} catch (ParseException e1) {
			throw new ExpectedException(EnemyErrorConstants.INCORRECT_DATE);
		}
		
		UserRequest userRequest = new UserRequest();
		userRequest.setFirstName(regResqForm.getFirstName());
		userRequest.setLastName(regResqForm.getLastName());
		userRequest.setEmail(regResqForm.getEmail());
		userRequest.setBornDate(date);
		userRequest.setGender(regResqForm.getGender());
		userRequest.setPass(Utils.MD5(password));	
	
		try {
			String hostName = request.getServerName();
			services.createUserRequest(userRequest, hostName);
		} catch (NoSuchAlgorithmException e) {
			throw new ExpectedException(EnemyErrorConstants.SERVICE_ERROR);
		} catch (UnsupportedEncodingException e) {
			throw new ExpectedException(EnemyErrorConstants.SERVICE_ERROR);
		} catch (AddressException e) {
		    throw new ExpectedException(EnemyErrorConstants.SERVICE_ERROR);
        } catch (MessagingException e) {
            throw new ExpectedException(EnemyErrorConstants.SERVICE_ERROR);
        } catch (DBException e) {
            throw new ExpectedException(EnemyErrorConstants.SERVICE_ERROR);
        } catch (InvalidUniqueBeanException e) {
            throw new ExpectedException(EnemyErrorConstants.SERVICE_ERROR);
        }
		
		return null;
	}
	
	private List<String> validate(RegisterRequestForm regResqForm){
		List<String> errors = new ArrayList<String>();
		
		if(Utils.isNullOrEmpty(regResqForm.getFirstName())){
			errors.add("Missing First Name");
		}
		if(Utils.isNullOrEmpty(regResqForm.getLastName())){
			errors.add("Missing Last Name");
		}
		if(Utils.isNullOrEmpty(regResqForm.getEmail())){
			errors.add("Missing Email");
		}
		if(Utils.isNullOrEmpty(regResqForm.getBornDate())){
			errors.add("Missing Birthdate");
		}
		if(Utils.isNullOrEmpty(regResqForm.getGender())){
			errors.add("Missing Gender");
		}
		if(Utils.isNullOrEmpty(regResqForm.getPassword())){
			errors.add("Missing Password");
		}
		if(Utils.isNullOrEmpty(regResqForm.getConfirmPassword())){
			errors.add("Missing Confirm Password");
		}
		
		return errors;
	}
}
